﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;
using Top.Api.Domain;

namespace AscentTopLibrary
{
    public class TopDbContext : DbContext
    {
        #region 构造函数
        protected TopDbContext()
        {

        }
        protected TopDbContext(System.Data.Entity.Infrastructure.DbCompiledModel model)
            : base(model)
        {

        }
        public TopDbContext(string nameOrConnectionString)
            : base(nameOrConnectionString)
        {

        }
        public TopDbContext(string nameOrConnectionString, System.Data.Entity.Infrastructure.DbCompiledModel model)
            : base(nameOrConnectionString, model)
        {

        }
        public TopDbContext(System.Data.Common.DbConnection existingConnection, bool contextOwnsConnection)
            : base(existingConnection, contextOwnsConnection)
        {

        }
        public TopDbContext(System.Data.Common.DbConnection existingConnection, System.Data.Entity.Infrastructure.DbCompiledModel model, bool contextOwnsConnection)
            : base(existingConnection, model, contextOwnsConnection)
        {

        }
        public TopDbContext(System.Data.Objects.ObjectContext objectContext, bool dbContextOwnsObjectContext)
            : base(objectContext, dbContextOwnsObjectContext)
        {

        }
        #endregion

        public DbSet<Item> Items { get; set; }
        public DbSet<Trade> Trades { get; set; }
        //public DbSet<CollectItem> CollectItems { get; set; }
        //public DbSet<User> Users { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Item>().HasKey(e => e.NumIid);
            modelBuilder.Entity<Item>().Property(e => e.NumIid).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);

            modelBuilder.Entity<Trade>().HasKey(e => e.Tid);
            modelBuilder.Entity<Trade>().Property(e => e.Tid).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);

            modelBuilder.Entity<Order>().HasKey(e => e.Oid);
            modelBuilder.Entity<Order>().Property(e => e.Oid).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);

            //modelBuilder.Entity<CollectItem>().HasKey(e => new {e.ItemNumid, e.UserNick});
            //modelBuilder.Entity<CollectItem>().Property(e => e.ItemNumid).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
            //modelBuilder.Entity<CollectItem>().Property(e => e.UserNick).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);

            modelBuilder.ComplexType<Top.Api.Domain.Location>();

            base.OnModelCreating(modelBuilder);
        }
    }
}
